﻿package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.InventoryList;
import com.edward.model.User;


public class InventoryListManager extends HibernateDaoSupport {

	public void addInventoryList(InventoryList inventoryList) {
		
		getHibernateTemplate().save(inventoryList);
	}
	
	public void updateInventoryList(InventoryList inventoryList) {
		
		getHibernateTemplate().update(inventoryList);
	}
	
	public void deleteInventoryList(int id){
		
		getHibernateTemplate().delete(getInventoryList(id));
	}
	
	public InventoryList getInventoryList(int id){
		InventoryList d =(InventoryList)getHibernateTemplate().get(InventoryList.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<InventoryList> listInventoryList(boolean oversea) {
		if(oversea){
			return getHibernateTemplate().find("from InventoryList i where i.warehouse in ('E','F','G')");
		}else{
			return getHibernateTemplate().find("from InventoryList i where i.warehouse in ('A','B','C')");
		}
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,boolean oversea) {
		List <InventoryList>list ;
		if(oversea){
			list = getHibernateTemplate().find("from InventoryList i where i.warehouse in ('E','F','G')"  );
		}else{
			list = getHibernateTemplate().find("from InventoryList i where i.warehouse in ('A','B','C')" );
		}
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InventoryList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public String getUserName(String name){
		List <User>list = getHibernateTemplate().find("from User u where u.username = '" + name +"'" );
		return list.get(0).getDescn();
		
	}
	



}



